package pers.fjl.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import pers.fjl.common.entity.QueryPageBean;
import pers.fjl.common.po.Commodity;
import pers.fjl.test.dao.GoodsDao;
import pers.fjl.test.service.GoodsService;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 商品管理对应的service实现
 *
 * @author fangjiale 2021年1月1日
 */
@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Commodity> implements GoodsService {
    @Resource
    private GoodsDao goodsDao;

    @Cacheable(value = {"GoodsMap"})
    public Page<Commodity> findPage(QueryPageBean queryPageBean) {
        Date time[] = new Date[2];
        int i = 0;
        for (Date date : queryPageBean.getDatetimepicker()) {
            time[i] = date;
            i++;
        }
        //设置分页条件
        Page<Commodity> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //设置查询条件
        QueryWrapper<Commodity> wrapper = new QueryWrapper<>();
        wrapper.like(queryPageBean.getQueryString() != null, "cname", queryPageBean.getQueryString())
                .eq(queryPageBean.getCompany() != null, "company", queryPageBean.getCompany())
                .eq(queryPageBean.getOrderStatus() != null, "order_status", queryPageBean.getOrderStatus())
                .eq(queryPageBean.getPaymentStatus() != null, "payment_status", queryPageBean.getPaymentStatus())
                .ge(time[0] != null, "promotion_start", time[0])
                .le(time[1] != null, "promotion_end", time[1]);
        page = goodsDao.selectPage(page, wrapper);
        return page;
    }

    @Override
    public boolean goodsStateChanged(Integer cid, boolean onSale) {
        boolean flag = update(new UpdateWrapper<Commodity>()
                .set("on_sale", onSale)
                .eq("cid", cid));
        return flag;
    }
}
